﻿using System.Web;

using Tszy.Unlimited.Base.Http;

namespace Tszy.Unlimited.Base.Extensions;

/// <summary>
/// Http 功能扩展。
/// </summary>
public static class HttpExtensions
{
    /// <summary>
    /// 将已进行 URL 编码的字符串进行解码。
    /// </summary>
    /// <param name="source">已进行 URL 编码的字符串。</param>
    /// <returns>解码后的字符串。</returns>
    public static string UrlDecode(this string source)
    {
        return HttpUtility.UrlDecode(source) ?? string.Empty;
    }

    /// <summary>
    /// 对字符串进行 URL 编码。
    /// </summary>
    /// <param name="source">字符串。</param>
    /// <returns>编码后的字符串。</returns>
    public static string UrlEncode(this string source)
    {
        return HttpUtility.UrlEncode(source) ?? string.Empty;
    }

    /// <summary>
    /// 将字典转换为 HTTP 请求参数形式的字符串。
    /// </summary>
    /// <param name="parameters"></param>
    /// <returns></returns>
    public static string GetHttpRequestParameters(this IDictionary<string, string> parameters)
    {
        if (parameters == null || parameters.Count <= 0)
        {
            return string.Empty;
        }

        var postData = string.Empty;

        foreach (var parameter in parameters)
        {
            if (!string.IsNullOrWhiteSpace(postData))
            {
                postData += "&";
            }

            var value = HttpUtility.UrlEncode(parameter.Value);

            postData += $"{parameter.Key}={value}";
        }

        return postData;
    }

    /// <summary>
    /// 发起 HTTP 请求。
    /// </summary>
    /// <param name="baseUrl"></param>
    /// <returns></returns>
    public static ITszyHttpClient Http(this string baseUrl)
    {
        return new TszyHttpClient(baseUrl);
    }

    /// <summary>
    /// 发起 HTTP GET 请求。
    /// </summary>
    /// <param name="baseUrl"></param>
    /// <param name="parameters">参数。</param>
    /// <returns></returns>
    public static ITszyHttpRequestGet HttpGet(this string baseUrl, object parameters)
    {
        return baseUrl.Http().Get(string.Empty, parameters);
    }

    /// <summary>
    /// 发起 HTTP GET 请求。
    /// </summary>
    /// <param name="baseUrl"></param>
    /// <param name="path"></param>
    /// <param name="parameters">参数。</param>
    /// <returns></returns>
    public static ITszyHttpRequestGet HttpGet(this string baseUrl, string path = "", object? parameters = null)
    {
        return baseUrl.Http().Get(path ?? string.Empty, parameters);
    }

    /// <summary>
    /// 发起 HTTP POST 请求。
    /// </summary>
    /// <param name="baseUrl"></param>
    /// <param name="parameters">参数。</param>
    /// <returns></returns>
    public static ITszyHttpRequestPost HttpPost(this string baseUrl, object parameters)
    {
        return baseUrl.Http().Post(string.Empty, parameters);
    }

    /// <summary>
    /// 发起 HTTP POST 请求。
    /// </summary>
    /// <param name="baseUrl"></param>
    /// <param name="path"></param>
    /// <param name="parameters">参数。</param>
    /// <returns></returns>
    public static ITszyHttpRequestPost HttpPost(this string baseUrl, string path = "", object? parameters = null)
    {
        return baseUrl.Http().Post(path ?? string.Empty, parameters);
    }

    /// <summary>
    /// 发起 HTTP POST JSON 请求。
    /// </summary>
    /// <param name="baseUrl"></param>
    /// <param name="parameters">参数。</param>
    /// <returns></returns>
    public static ITszyHttpRequestPostJson HttpPostJosn(this string baseUrl, object parameters)
    {
        return baseUrl.Http().PostJson(string.Empty, parameters);
    }

    /// <summary>
    /// 发起 HTTP POST JSON 请求。
    /// </summary>
    /// <param name="baseUrl"></param>
    /// <param name="path"></param>
    /// <param name="parameters">参数。</param>
    /// <returns></returns>
    public static ITszyHttpRequestPostJson HttpPostJosn(this string baseUrl, string path = "", object? parameters = null)
    {
        return baseUrl.Http().PostJson(path ?? string.Empty, parameters);
    }
}